/********************************************************************************
Script name	: StarterKit.CreateLogin
Description	: Create login and set default database
********************************************************************************/

USE [Master]
GO

IF NOT EXISTS(SELECT sid FROM master.dbo.syslogins WHERE name = N'$(UserID)')
BEGIN
	EXECUTE sp_executesql 
	N'CREATE LOGIN $(UserID) WITH PASSWORD=N''$(UserPassword)'', 
	DEFAULT_DATABASE=$(StarterKitDb), CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF'
END
GO


USE $(StarterKitDb)
GO

IF NOT EXISTS(SELECT users.sid FROM master.dbo.syslogins AS logins
	JOIN dbo.sysusers users ON logins.sid = users.sid
	WHERE logins.loginname = N'$(UserID)' 
	AND users.name = N'$(UserID)')
BEGIN
	EXECUTE sp_executesql N'CREATE USER [$(UserID)] FOR LOGIN [$(UserID)]'
	
	EXEC sp_addrolemember N'db_owner', N'$(UserID)'
END
GO